﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using Model;

namespace Logic
{
     public class ItemsTypeLogic
    {

        public List<Sys_Items> GetList()
        {
            Model1Container db = new Model1Container();
          //  return new RepositoryBase<Sys_Items>(new SysAdmin()).IQueryable().ToList();
            return db.Set<Sys_Items>().ToList(); 
        }
        public List<Sys_Items> GetChildListByParentID(string ParentID)
        {
            Model1Container db = new Model1Container();
            return db.Set<Sys_Items>().Where(c => c.F_ParentId == ParentID).ToList();
        }
        public Sys_Items GetForm(string keyValue)
        {
            Model1Container db = new Model1Container();
            return db.Set<Sys_Items>().Find(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            Model1Container db = new Model1Container();

            if (db.Set<Sys_Items>().Count(t => t.F_ParentId.Equals(keyValue)) > 0)
            {
                throw new Exception("删除失败！操作的对象包含了下级数据。");
            }
            else
            {
                var entitys = db.Set<Sys_Items>().Where(t => t.F_Id == keyValue).ToList();
                entitys.ForEach(m => db.Entry<Sys_Items>(m).State = EntityState.Deleted);
                db.SaveChanges();
            }
        }
        public void SubmitForm(Sys_Items itemsEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                using (Model1Container db = new Model1Container())
                {
                    itemsEntity.F_Id = keyValue;
                    db.Sys_Items.Attach(itemsEntity);
                    db.Entry(itemsEntity).State = EntityState.Modified;
                    db.SaveChanges();
                }
            }
            else
            {
                using (Model1Container db = new Model1Container())
                {
                    itemsEntity.F_Id = Guid.NewGuid().ToString();
                    db.Sys_Items.Add(itemsEntity);
                    db.SaveChanges();
                }
            }
        }
    }
}
